package org.goodsmanager.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 购物车实体类
 *
 * @author Goods Manager System
 * @version 1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "cart")
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 用户ID
     */
    @Column(name = "user_id", nullable = false)
    private String userId;

    /**
     * 商品ID
     */
    @Column(name = "product_id", nullable = false)
    private String productId;

    /**
     * 商品名称
     */
    @Column(name = "product_name", nullable = false)
    private String productName;

    /**
     * 商品图片
     */
    @Column(name = "product_image")
    private String productImage;

    /**
     * 商品单价
     */
    @Column(name = "product_price", nullable = false)
    private Double productPrice;

    /**
     * 购买数量
     */
    @Column(name = "quantity", nullable = false)
    private Integer quantity;

    /**
     * 商品库存
     */
    @Column(name = "stock_quantity")
    private Integer stockQuantity;

    /**
     * 商品状态（1-在售，0-下架）
     */
    @Column(name = "product_status")
    private Integer productStatus;

    /**
     * 选中状态（1-选中，0-未选中）
     */
    @Column(name = "selected", nullable = false)
    private Boolean selected = true;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    /**
     * 计算小计金额
     */
    public Double getSubtotal() {
        return productPrice * quantity;
    }

    /**
     * 检查商品是否有效（在售且有库存）
     */
    public Boolean isValid() {
        return productStatus != null && productStatus == 1 && 
               stockQuantity != null && stockQuantity > 0;
    }
}